package com.shire.shire_interface.controller.phrase;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.phrase.WxPhraseReply;
import com.shire.shire_interface.model.phrase.WxPhraseReplyExample;
import com.shire.shire_interface.service.phrase.WxPhraseReplyService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/phrase")
public class WxPhraseReplyController extends BaseController {
    @Resource
    private WxPhraseReplyService phraseReplyService;

    @PostMapping("/saveReplyPhrase")
    public Response savePhraseReply(@RequestBody WxPhraseReply phraseReply, HttpSession session){
        Response response = new Response();
        try{
            if (!checkLoginUserByCsId(session, phraseReply.getCsId())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            phraseReplyService.saveReplyPhrase(phraseReply);
            response.setData(phraseReply);
            response.success("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }

    @PostMapping("/getReplyPhrase")
    public Response getReplyPhrase(@RequestBody JSONObject queryObject, HttpSession session){
        Response response = new Response();
        try{
            Integer csId = queryObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            Integer pageSize = 50;
            if (queryObject.containsKey("pageSize"))
                pageSize = queryObject.getInteger("pageSize");
            WxPhraseReplyExample example = new WxPhraseReplyExample();
            example.createCriteria().andCsIdEqualTo(csId);
            example.setLimitStart(0);
            example.setLimitSize(pageSize);
            example.setOrderByClause("id desc");

            response.setData(phraseReplyService.selectByExample(example));
            response.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }

    @PostMapping("/deleteReplyPhase")
    public Response deleteReplyPhrase(@RequestBody JSONObject queryObject, HttpSession session){
        Response response = new Response();
        try{
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            phraseReplyService.deleteById(queryObject.getInteger("id"));

            response.success("删除成功");
        }catch (Exception e){
            response.error(e.getMessage());
        }
        return response;
    }
}
